import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class GameScreen extends JFrame {
	//move button
	JButton moveButton;
	
	//order of first dot
	JTextField firstDot;
	
	//order of second dot
	JTextField secondDot;
	
	//Timer
	JTextField timer;
	
	//reference to the Game object (the parent)
	Game game;
	
	//history
	JTextArea history;
	
	//Drawing frame
	ConnectTheDotsDrawingFrame drawingFrame;
	
	//message from the game
	JTextField message;
	
	//the thread to run the game
	GameRunThread grt;
	
	public boolean gameEnded;
	
	//current player turn: 1 for player, -1 for computer
	public int turn;
	
	public boolean moveButtonClicked;
	
	public GameScreen(Game game)
	{
		this.game = game;
		moveButton = new JButton("Move");
		moveButton.addActionListener(new MoveButtonHandler());
		firstDot = new JTextField(5);
		secondDot = new JTextField(5);
		message = new JTextField(90);
		
		drawingFrame = new ConnectTheDotsDrawingFrame(game);
		setLayout(new BorderLayout());
		
		//panel for the south area
		JPanel southPanel = new JPanel(new GridLayout(2, 1));
		JPanel southUpper = new JPanel(new FlowLayout());
		JPanel southLower = new JPanel(new FlowLayout());
		
		southUpper.add(message);
		
		southLower.add(new JLabel("First Dot: "));
		southLower.add(firstDot);
		southLower.add(new JLabel("Second Dot: "));
		southLower.add(secondDot);
		southLower.add(moveButton);
		
		southPanel.add(southUpper);
		southPanel.add(southLower);
		
		//add the south panel to the south area
		add(southPanel, BorderLayout.SOUTH);
		
		//add the drawing area to the center
		add(drawingFrame, BorderLayout.CENTER);
		
		//add history area
		history = new JTextArea(20, 20);
		history.setEnabled(true);
		add(history, BorderLayout.EAST);
		
		//add timer
		timer = new JTextField(5);
		add(timer, BorderLayout.NORTH);
		
		firstDot.requestFocus();
	}
	
	public void initScreen()
	{
		firstDot.setText("");
		secondDot.setText("");
		
		//make a new board for the game with the info got from the user
		Board board = new Board(game.getStartScreen().getBoardHeight(), game.getStartScreen().getBoardWidth());
		//set the new board to the game
		game.setBoard(board);
		
		//if the player go first, call playerMove
		this.turn = game.getStartScreen().getGoFirst();
		
		//if the player move first
		if (turn == 1)
		{
			firstDot.setText("");
			firstDot.setEnabled(true);
			moveButton.setEnabled(true);
			message.setText("Please make your first move");
			moveButtonClicked = false;
		
			secondDot.setText("");
			secondDot.setEnabled(true);
		}
		else //if computer moves first
		{
			firstDot.setText("");
			firstDot.setEnabled(false);
			moveButton.setEnabled(false);
			message.setText("Computer is making its first move");
			moveButtonClicked = false;
		
			secondDot.setText("");
			secondDot.setEnabled(false);
		}
		
		gameEnded = false;
	}
	
	//run the game
	public void runGameScreen()
	{
		//create 1 thread that constantly checking the status of the turn variable
		grt = new GameRunThread(this);
		grt.start();
	}
	
	private class MoveButtonHandler implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			moveButtonClicked = true;
		}
	}
}